home *** CD-ROM | disk | FTP | other *** search
- /* Scheme In One Define.
-
- The garbage collector, the name and other parts of this program are
-
- * COPYRIGHT (c) 1989 BY *
- * PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS. *
-
- Conversion to full scheme standard, characters, vectors, ports, complex &
- rational numbers, and other major enhancments by
-
- * Scaglione Ermanno, v. Pirinoli 16 IMPERIA P.M. 18100 ITALY *
-
- Permission to use, copy, modify, distribute and sell this software and its
- documentation for any purpose and without fee is hereby granted, provided
- that the above copyright notice appear in all copies and that both that
- copyright notice and this permission notice appear in supporting
- documentation, and that the name of Paradigm Associates Inc not be used in
- advertising or publicity pertaining to distribution of the software without
- specific, written prior permission.
-
- PARADIGM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
- ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
- PARADIGM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
- ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
- IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
- OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
- */
-
- #include <ctype.h>
- #include <setjmp.h>
- #include <signal.h>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "siod.h"
-
-
- LISP portcons(FILE *fp)
- {long flag;
- LISP s,f;
- flag=no_interrupt(1);
- NEWCELL(s,tc_port)
- PORTPTR(s) = fp;
- PORTFLAG(s) = 0;
- no_interrupt(flag);
- return(s);}
-
- LISP openport(LISP name,LISP how,LISP buf)
- {if(NSTRINGP(name))err("open-port",name,ERR_FIRST | ERR_NSTR);
- if(NSTRINGP(how))err("open-port",how,ERR_SECOND | ERR_NSTR);
- if(NINTNUMP(buf))err("open-port",buf,ERR_THIRD | ERR_NINT);
- return(open_port(SNAME(name),SNAME(how),INTNM(buf)));}
-
- LISP portp(LISP x)
- {if(PORTP(x))return(truth);
- return(NIL);}
-
- LISP open_port(char *name,char *how,int buf)
- {LISP z;
- long flag;
- FILE *fp;
- flag = no_interrupt(1);
- if(strlen(how)>3)
- *(how+3)='\0';
- fp = fopen(name,how);
- if (!fp)
- err(name,NIL,ERR_NFIL);
- if(buf>0)
- setvbuf(fp,NULL,_IOFBF,BUFSIZ);
- else if(buf<0)
- setvbuf(fp,NULL,_IOLBF,BUFSIZ);
- else
- setvbuf(fp,NULL,_IONBF,BUFSIZ);
- z = portcons(fp);
- if(strchr(how,'+'))
- PORTFLAG(z)=0;
- else if(strchr(how,'r'))
- PORTFLAG(z)=1;
- else
- PORTFLAG(z)=-1;
- no_interrupt(flag);
- return(z);}
-
- LISP input_portp(LISP port)
- {if(PORTP(port) && (PORTFLAG(port)>=0))
- return(truth);
- return(NIL);}
-
- LISP output_portp(LISP port)
- {if(PORTP(port) && (PORTFLAG(port)<=0))
- return(truth);
- return(NIL);}
-
- LISP file_exist(LISP x)
- {FILE *fp;
- if NSTRINGP(x) err("file-exists?",x,ERR_GEN_ARG|ERR_NSTR);
- fp = fopen(SNAME(x),"r");
- if (!fp)
- return(NIL);
- fclose(fp);
- return(truth);}
-
- LISP lflush(LISP po)
- {if(NPORTP(po))
- err("flush-port",po,ERR_GEN_ARG | ERR_NPOR);
- clearerr(PORTPTR(po));
- if(fflush(PORTPTR(po))==EOF)
- err("IO error during flush-port",NIL,ERR_GEN);
- return(NIL);}
-
- LISP close_port(LISP port)
- {long flag;
- if(NPORTP(port))err("close-port",port,ERR_GEN_ARG | ERR_NPOR);
- if(PORTPTR(port)==NULL) return(truth);
- flag = no_interrupt(1);
- if (fclose(PORTPTR(port)))
- err("could not close a file",NIL,ERR_GEN);
- PORTPTR(port) = NULL;
- no_interrupt(flag);
- return(truth);}
-
- LISP vload(char *fname,LISP env)
- {LISP form,port;
- FILE *f;
- port = open_port(fname,"r",1);
- f = PORTPTR(port);
- while(1)
- {form = lreadf(f);
- if EQ(form,eof_val) break;
- leval(form,env);}
- close_port(port);
- return(NIL);}
-
- LISP load(LISP form,LISP env)
- {LISP fname,nenv;
- fname = leval(car(form),env);
- nenv = leval(car(cdr(form)),env);
- if NSTRINGP(fname) err("load",fname,ERR_FIRST | ERR_NSTR);
- if(NULLP(nenv))
- nenv=env;
- else if(EQ(nenv,sym_user_environment))
- nenv=NIL;
- else if(NENVP(nenv)) err("load",nenv,ERR_SECOND | ERR_NENV);
- return(vload(SNAME(fname),nenv));}
-
- LISP getfileposition(LISP port)
- {LISP pos;
- long po;
- if(NPORTP(port))err("get-file-position",port,ERR_GEN_ARG | ERR_NPOR);
- po = ftell(PORTPTR(port));
- if(po == EOF) err("I/O error occurred during get-file-position",NIL,ERR_GEN);
- pos = intcons(po);
- return(pos);}
-
- LISP setfileposition(LISP port,LISP byte,LISP origin)
- {long po;
- if(NPORTP(port))err("set-file-position!",port,ERR_FIRST | ERR_NPOR);
- if(NINTNUMP(byte))err("set-file-position!",byte,ERR_SECOND | ERR_NINT);
- if(NINTNUMP(origin))err("set-file-position!",origin,ERR_THIRD | ERR_NINT);
- switch(INTNM(origin))
- {case 0: po = SEEK_SET;
- break;
- case 1: po = SEEK_CUR;
- break;
- case 2: po = SEEK_END;
- break;}
- if(fseek(PORTPTR(port),INTNM(byte),po))
- err("I/O error occurred during set-file-position!",NIL,ERR_GEN);
- return(truth);}
-